﻿using ShinePipe;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PipeServer
{
    public partial class FrmServer : Form
    {
        ShineNamePipeServer _pipeServer;

        public FrmServer()
        {
            InitializeComponent();
            _pipeServer = new ShineNamePipeServer();
            _pipeServer.ShinePipeRecvEvent += _pipeServer_ShinePipeRecvEvent;
            _pipeServer.ShinePipeDebugEvent += _pipeServer_ShinePipeDebugEvent;
            _pipeServer.ShinePipeCloseEvent += _pipeServer_ShinePipeCloseEvent;
        }

        void _pipeServer_ShinePipeCloseEvent(object sender, ShinePipeCloseEventArgs args)
        {
            this.Invoke(new EventHandler(delegate
           {
               this.btnStart.Enabled = true;
               this.btnStop.Enabled = false;
               this.btnSend.Enabled = false;
               this.tbxPipeName.Enabled = true;
           }));
        }

        void _pipeServer_ShinePipeDebugEvent(object sender, ShinePipeDebugEventArgs args)
        {
            this.Invoke(new EventHandler
                (delegate 
                    {
                        this.tbxInfo.Text += args.Message + "\r\n";
                    }
                ));
        }

        void _pipeServer_ShinePipeRecvEvent(object sender, ShinePipeRecvEventArgs args)
        {
            this.Invoke(new EventHandler(delegate { this.tbxReceive.AppendText(args.Message + "\r\n"); }));
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            _pipeServer.Start(this.tbxPipeName.Text.Trim());

            this.btnStart.Enabled = false;
            this.btnStop.Enabled = true;
            this.btnSend.Enabled = true;
            this.tbxPipeName.Enabled = false;
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            _pipeServer.Stop();

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            _pipeServer.Write(this.tbxSend.Text.Trim());
        }



        private void FrmServer_Load(object sender, EventArgs e)
        {

        }


    }
}
